package store.wetravel.web.backend.category.action;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import store.wetravel.common.Const;
import store.wetravel.common.ResponseCode;
import store.wetravel.common.ServerResponse;
import store.wetravel.pojo.Category;
import store.wetravel.web.backend.category.service.ICategoryService;
import store.wetravel.pojo.User;
import store.wetravel.web.user.service.IUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by xinlong on 2017/10/25.
 */
@Controller
@RequestMapping("/manage/category/")
public class CategoryManageController {


    @Autowired
    @Qualifier("iUserService")
    private IUserService iUserService;

    @Autowired
    private ICategoryService iCategoryService;

    @RequestMapping("add_category.do")
    @ResponseBody
    public ServerResponse addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId",defaultValue = "0") int parentId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        //校验是否是管理员
        if(iUserService.checkAdminRole(user).isSuccess()){
            //是管理员
            //增加我们的处理分类逻辑
            return iCategoryService.addCategory(categoryName,parentId);
        }else{
            return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
        }
    }

    @RequestMapping("set_category_name.do")
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session,Integer categoryId,String categoryName){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        //校验是否是管理员
        if(iUserService.checkAdminRole(user).isSuccess()){
            //更新category
            return iCategoryService.updateCategoryName(categoryId,categoryName);
        }else{
            return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
        }
    }

    @RequestMapping("get_category.do")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        //校验是否是管理员
        if(iUserService.checkAdminRole(user).isSuccess()){
            //查询子节点的category信息，并且不递归，保持平级
            return iCategoryService.getChildrenParallelCategory(categoryId);
        }else{
            return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
        }
    }

    @RequestMapping("get_deep_category.do")
    @ResponseBody
    public ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session,@RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        //校验是否是管理员
        if(iUserService.checkAdminRole(user).isSuccess()){
            //查询当前节点的id和递归子节点的id
            return iCategoryService.selectCategoryAndChildrenById(categoryId);
        }else{
            return ServerResponse.createByErrorMessage("无权限操作，需要管理员权限");
        }
    }

    @RequestMapping("{parentId}")
    public String categoryContentPage(@PathVariable("parentId")Integer parentId, HttpSession session, HttpServletRequest request){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        //校验是否是管理员
        if(user == null || iUserService.checkAdminRole(user).isError()){
            return "backend/manageLogin";
        }else {
            //查询子节点的category信息，并且不递归，保持平级
            ServerResponse<List<Category>> serverResponse = iCategoryService.getChildrenParallelCategory(parentId);
            request.setAttribute("serverResponse",serverResponse);
            //递归查询父节点的category信息直到根节点(包括)
            ServerResponse<List<Category>> parentLink = iCategoryService.selectCategoryAndParentById(parentId);
            request.setAttribute("parentLink",parentLink);
            //将父节点id放入request中方便添加子节点
            request.setAttribute("parentId",parentId);
            return "backend/category/content";
        }
    }

    @RequestMapping("{parentId}/add/{categoryName}")
    public String addCategoryPage(HttpSession session, @PathVariable("categoryName") String categoryName, @PathVariable("parentId") int parentId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        //校验是否是管理员
        if(user == null || iUserService.checkAdminRole(user).isError()){
            return "backend/manageLogin";
        }else {
            iCategoryService.addCategory(categoryName,parentId);
            return "redirect:/manage/category/"+parentId;
        }
    }

    @RequestMapping("{parentId}/delete/{categoryId}")
    public String deleteCategoryPage(HttpSession session, @PathVariable("categoryId") Integer categoryId, @PathVariable("parentId") Integer parentId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        //校验是否是管理员
        if(user == null || iUserService.checkAdminRole(user).isError()){
            return "backend/manageLogin";
        }else {
            iCategoryService.deleteCategory(categoryId);
            return "redirect:/manage/category/"+parentId;
        }
    }

    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String updateCategoryPage(HttpSession session, @RequestParam("parentId") Integer parentId,@RequestParam("categoryId")Integer categoryId, @RequestParam("categoryName")String categoryName) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        //校验是否是管理员
        if (user == null || iUserService.checkAdminRole(user).isError()) {
            return "backend/manageLogin";
        }else if(parentId == null || parentId < 0 || categoryId == null || categoryId < 0 || StringUtils.isEmpty(categoryName)){
            return "error404";
        }else {
            iCategoryService.updateCategoryName(categoryId, categoryName);
            return "redirect:/manage/category/" + parentId;
        }
    }

}
